package com.sobjal.android;

import java.util.ArrayList;

import java.util.HashMap;

import com.sobjal.android.utils.SmartObjects;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Shows all types of device to the user
 * 
 * @author Fabio Monteiro
 *
 */
public class listType extends Activity {

	private  ListView listView;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewtype);
        this.setTitle("Select a type of device");
        
        
        ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
      
        	//add of the two types of devices to the list ; TODO : get types from the user's smartobjects maybe
            HashMap<String, Object> user = new HashMap<String, Object>();
            user.put("img", R.drawable.nabaztag);
            user.put("deviceType", "Nabaztag");
            users.add(user);
            
            HashMap<String, Object> user2 = new HashMap<String, Object>();
            user2.put("img", R.drawable.mydeskfriend);
            user2.put("deviceType", "Mydeskfriend");
            users.add(user2);
        
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                users,
                R.layout.selecttypedevice,		//TO CHECK if the layout still works 
                new String[] {  "deviceType","img"},

                new int[] { R.id.name, R.id.img });
       
        listView=(ListView) findViewById(R.id.listtype); ////TO CHECK if the layout still works
        listView.setAdapter(saImageItems);
        listView.setOnItemClickListener(new OnItemClickListener(){

        	public void onItemClick(AdapterView<?> parent, View view, int position,
        			long id) {
        		HashMap<String, Object> selectedType= 
        			(HashMap<String, Object>) parent.getItemAtPosition(position);

        		String deviceType = (String) selectedType.get("deviceType");

        		if(SmartObjects.checkIfTypeInTheList(deviceType, SmartObjects.SMART_OBJECTS_LIST)){

        			Bundle b = new Bundle();
        			b.putString("deviceType", (String) selectedType.get("deviceType"));

        			Intent intent = new Intent();
        			intent.putExtras(b);

        			intent.setClass(listType.this, ListSmartByType.class); 
        			startActivity(intent);
        		}else{
        			Toast.makeText(getApplicationContext(), "You have no device corresponding to that type.", Toast.LENGTH_LONG).show();
        		}
        	}

        });

	}
// action pour menu

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 
		LayoutInflater  factory = LayoutInflater.from(this);
		final View textEntryView = factory.inflate(R.layout.dialog_about, null);
		 
		switch (item.getItemId()) {
		case R.id.menulogin:
			// about function
			if (SmartObjects.SMART_OBJECTS_LIST.isEmpty()) {
				Intent intent = new Intent();
				intent.setClass(listType.this, SobjalMain.class);
				startActivity(intent);
			} else {
				Intent intent = new Intent();
				intent.setClass(listType.this, login.class);
				startActivity(intent);
			}
			return true;
		case R.id.menulogoff:
			// settings function
			new AlertDialog.Builder(this)
					.setMessage("Are you sure you want to exit?")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									SmartObjects.SMART_OBJECTS_LIST = null;
									Intent intent = new Intent();
									intent.setClass(listType.this,
											SobjalMain.class);
									startActivity(intent);
									listType.this.finish();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							}).show();
			return true;
		case R.id.menuabout:
			
			new AlertDialog.Builder(this)
			.setMessage("About")
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setView(textEntryView).show();
		default:
			Log.e("Unknown menu item selected", " ");
			return false;
		}
	}
}
